<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Ratings
 *
 * @author miroslav
 */

//include_once dirname(__FILE__).'/db_connect.php';



class ratings {

    private $widget_id;
    private $data = array();
    private $conn;

    function __construct($wid) {
        //$wid = substr($wid, 1);
        $this->widget_id = $wid;

//        $db = new db_connect();
//        $this->conn = $db->connect();
    }

    public function get_ratings_old() {
        if ($this->data[$this->widget_id]) {
            echo json_encode($this->data[$this->widget_id]);
        } else {
            $data['widget_id'] = $this->widget_id;
            $data['number_votes'] = 0;
            $data['total_points'] = 0;
            $data['dec_avg'] = 0;
            $data['whole_avg'] = 0;
            echo json_encode($data);
        }
    }

    public function vote_old() {
        preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
        $vote = $match[1];


        $ID = $this->widget_id;
        if ($this->data[$ID]) {
            $this->data[$ID]['number_votes'] += 1;
            $this->data[$ID]['total_points'] += $vote;
        } else {
            $this->data[$ID]['number_votes'] = 1;
            $this->data[$ID]['total_points'] = $vote;
        }

        $this->data[$ID]['dec_avg'] = round($this->data[$ID]['total_points'] / $this->data[$ID]['number_votes'], 1);
        $this->data[$ID]['whole_avg'] = round($this->data[$ID]['dec_avg']);

        file_put_contents($this->data_file, serialize($this->data));
        $this->get_rateings();
    }

    /*
     * @assert (10) == "{"widget_id":10,"number_votes":0,"total_points":0,"dec_avg":0,"whole_avg":0}"
     * @assert (11) == "{"widget_id":11,"number_votes":0,"total_points":0,"dec_avg":0,"whole_avg":0}"
     * @assert (13) == "{"widget_id":13,"number_votes":0,"total_points":0,"dec_avg":0,"whole_avg":0}"
     *
     */
    public function get_rateings() {
        $ID = $this->widget_id;
//        $sql = "SELECT RateNum, RateSum FROM rateing WHERE IDRate = '$ID';"; //write query
//
//        $result = mysql_query($sql, $this->conn) or die(mysql_error());
//        if ($result) {
//            $row = mysql_fetch_array($result);
//            $this->data["widget_id"] = $ID;
//            $this->data["number_votes"] = $row["RateNum"];
//            $this->data["total_points"] = $row["RateSum"];
//            $this->data["dec_avg"] = round($row["RateSum"] / $row["RateNum"], 1);
//            $this->data["whole_avg"] = round($row["RateSum"] / $row["RateNum"]);
//        } else {
            $this->data["widget_id"] = $ID;
            $this->data["number_votes"] = 0;
            $this->data["total_points"] = 0;
            $this->data["dec_avg"] = 0.0;
            $this->data["whole_avg"] = 0;
//        }
        echo json_encode($this->data);
        return json_encode($this->data);
    }

    public function vote() {
        if ($this->wasVoted()) {
            preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
            $vote = $match[1];

            $ID = $this->widget_id;
            $select_query = "SELECT RateNum, RateSum FROM rateing WHERE IDRate = '$ID';"; //vraca vrednosti koje treba povecati (broj glasanja i ukupan broj glasova)


            $result = mysql_query($select_query, $this->conn);
            $row = mysql_fetch_array($result);
            $rateNum = $row['RateNum'] + 1;
            $rateSum = $row['RateSum'] + $vote;

            $update_query = "UPDATE rateing SET RateNum = '$rateNum', RAteSum = '$rateSum' WHERE IDRate = '$ID'"; //povecava te vrednosti za 1 i upisuje nazad u bazu
            mysql_query($update_query, $this->conn);

            $this->get_rateings();
        }
    }

    private function wasVoted() {
        //treba implementirati proveru da li je osoba vec glasala
        return true;
    }


    public function vote_nonDb() {
        preg_match('/star_([1-5]{1})/', $_POST['clicked_on'], $match);
        $vote = $match[1];

        $this->data["widget_id"] = $this->widget_id;
        $this->data["number_votes"] = 1;
        $this->data["total_points"] = $vote;
        $this->data["dec_avg"] = $vote;
        $this->data["whole_avg"] = $vote;
        
        echo json_encode($this->data);
    }

}

?>
